﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mail;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using Eis51.CampanyCMS.Model.BaseInfoMng;
using Eis51.CampanyCMS.Model;
namespace Eis51.CampanyCMS.Business.Bus
{
  public   class SendMailBLL
  {
     #region 自动生成代码：取得相关实体

		/// <summary>
		/// 方法名称: ProductsTypeBLL
		/// 内容摘要: 构造函数进行初始化
		/// </summary>
         protected SendMailBLL()
		{
		}
         private static volatile SendMailBLL m_instance = null;
		/// <summary>
		/// 方法名称: GetInstance
		/// 内容摘要: 取得 ProductsTypeBLL 对象
		/// </summary>
		/// <returns>返回一个已经存在的实体</returns>
         public static SendMailBLL GetInstance()
		{
			// 通用的必要代码 iBatisNet双校检机制,如果实例不存在
			if (m_instance == null)
			{
                lock (typeof(SendMailBLL))
				{

					// 如果实例不存在
					if (m_instance == null)
					{

						// 创建一个的实例
                        m_instance = new SendMailBLL();
					}
				}
			}

			// 返回业务逻辑对象
			return m_instance;
		}
		
		#endregion

         public string MailTo = "";
         public string MailFrom = "";
         public string Subject = "";
         public string Body = "";
         public string Password = "";
         public string SmtpServer = "";
         public int IsHtml = 1;
         public string Bcc = "";
         public void sendMailMsg()
         {
             MailMessage mail = new MailMessage();
             mail.To = this.MailTo;
             if (!string.IsNullOrEmpty(this.Bcc))
                        mail.Bcc = this.Bcc;
             mail.From = this.MailFrom;
             mail.Subject = this.Subject;
             mail.Body = this.Body;
             mail.BodyFormat = this.IsHtml == 1 ? MailFormat.Html : MailFormat.Text;//设置为HTML格式 
             mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
             mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendemailaddress", this.MailTo);
             mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpaccountname", this.MailFrom);
             //设置为需要用户验证
             mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
             //设置验证用户名(把my_username_here改为你的验证用户名） 
             mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.MailFrom);
             //设置验证密码（把password改为你的验证密码） 
             mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.Password);
             SmtpMail.SmtpServer = SmtpServer;
             SmtpMail.Send(mail);
         }

         public void OrderSendMail(OrderApply orderapply, string body )
         {
             Emailsever emailsever = EmailseverBLL.GetInstance().GetEmailseverByCreateBy(orderapply.ProductSeller);
             if (emailsever != null && emailsever.Issend > 0)
             {
                 string Orderno = orderapply.OrderNo;
                 double tMoney= orderapply.MailMoney + orderapply.PreferentialMoney + orderapply.ProductTMoney;
                 this.Bcc = emailsever.Emailto;
                 this.MailFrom = emailsever.Emailfrom;
                 string ReEmial = orderapply.ReceiveEmail;
                 if (string.IsNullOrEmpty(ReEmial))
                 {
                   ResUser r= ResUserBLL.GetInstance().GetUser(orderapply.ResUserNo);
                   if (r != null)
                   {
                       ReEmial = r.Email;
                   }
                 }
                 if (string.IsNullOrEmpty(ReEmial))
                 {
                     ReEmial = emailsever.Emailto;
                 }
                 this.MailTo = ReEmial;
                 this.SmtpServer = emailsever.Smtp;
                 this.Password = emailsever.Password;

                 string strbody = string.Format(@"尊敬的{4} ,<br>&nbsp; &nbsp;&nbsp;&nbsp; 感谢您对PBX产品的信任,我们会尽快处理您提交的订单{0},如您对本次交<br>
                                 易有任何疑问,请及时致电400-0061-661或者传真86-755-22643263.<br>PBX客服人员非常愿意给您提供相关帮助. <br> ------------------------- <br>订单号：{0}<br>状态：已付款  <br> 产品列表信息： {1}  <br> 收货地址信息：{2}
                                <br>邮寄方式信息： {3}  <br>  ------------------------  <br>  再次感谢您选择PBX产品!  <br><br><br>
                                <br><span style='font-size: x-small'> PBX中国区总代理</span>
                                <br><span style='font-size: x-small'> 深圳市贝力源科技有限公司</span>
                                <br><span style='font-size: x-small'>地址：中国深圳宝安石岩北环路西28号东原工业园</span>
                                <br><span style='font-size: x-small'>邮编：518108</span>
                                <br><span style='font-size: x-small'>总机：+86-755-81792888</span>
                                <br><span style='font-size: x-small'>传真：+86-755-22643263 </span>", Orderno, body, orderapply.ReceiveAddress, orderapply.Shipment, orderapply.ResUserNo);

                 this.Body = strbody;
                 this.Subject = "您的PBX订单--" + Orderno;
                 if (ReEmial != string.Empty)
                 {
                     this.sendMailMsg();
                 }
               
             }
         }



         public void PaySendMail(string ReceiveEmail,string Money )
         {
             Emailsever emailsever = EmailseverBLL.GetInstance().GetEmailseverByCreateBy("dy001");
             if (emailsever != null && emailsever.Issend > 0)
             {
                
                 this.Bcc = emailsever.Emailto;
                 this.MailFrom = emailsever.Emailfrom;
                 string ReEmial = ReceiveEmail;
                 
               
                 this.MailTo = ReEmial;
                 this.SmtpServer = emailsever.Smtp;
                 this.Password = emailsever.Password;
                 this.Body = string.Format(@"尊敬的 {1}用户，<br> &nbsp; &nbsp; &nbsp; &nbsp;您在PBX网站充值已成功，本次充值金额为：{0} 元。
                                                 <br>如有疑问，请致电PBX全国服务热线400-0061-661咨询，PBX客服人员随时给您提供相关帮助！ 
                                                  <br> <br><br> <span style='font-size: x-small'>PBX中国区总代理 </span> <br> <span style='font-size: x-small'> 深圳市贝力源科技有限公司  </span> 
                                                 <br> <span style='font-size: x-small'>地址：中国深圳宝安石岩北环路西28号东原工业园 </span>  <br><span style='font-size: x-small'> 邮编：518108 </span>  <br> <span style='font-size: x-small'>总机：+86-755-81792888 </span>   <br>  <span style='font-size: x-small'> 传真：+86-755-22643263</span> ", Money, ReceiveEmail);
                 this.Subject =  "PBX充值邮件 " ; 
                 
                
                 if (ReEmial != string.Empty)
                 {
                     this.sendMailMsg();
                 }

             }
         }

         public bool  GetPassword(string Email,string createby)
         {
             Emailsever emailsever = EmailseverBLL.GetInstance().GetEmailseverByCreateBy(createby);
             ResUser r =  ResUserBLL.GetInstance().GetUserByEmail(Email);
             if (r != null)
             {
                 // this.Bcc = Email;
                 this.MailFrom = emailsever.Emailfrom;
                 this.MailTo = Email;
                 this.SmtpServer = emailsever.Smtp;
                 this.Password = emailsever.Password;
                 this.Body = string.Format(@"尊敬的 {0}用户，<br> &nbsp; &nbsp; &nbsp; &nbsp;您在PBX网站注册的用户名：{0}  密码为：{1} <br> 如需，您可以在登陆PBX网站（www.pbxaudio.com）后更改您的密码。敬请妥善保护好您的登陆密码。
                                   <br> 如有疑问，请致电PBX服务热线，+86-755-27640888x8141查询，谢谢！ <br> &nbsp; &nbsp; &nbsp; &nbsp;Best Regards, <br>&nbsp; &nbsp; &nbsp; &nbsp;  PBX AUDIO (CHINA)", r.UserName, r.PassdWod);
                 this.Subject = string.Format("PBX注册用户密码提示邮件 "); 
                 this.sendMailMsg();
                 return true;
             }
             else
             {
                 return false;
             }

         }


         public bool RegSendMail( ResUser r, string createby, Coupon c)
         {
             Emailsever emailsever = EmailseverBLL.GetInstance().GetEmailseverByCreateBy(createby);
             
             if (r != null&& c!=null )
             {
                 // this.Bcc = Email;
                 this.MailFrom = emailsever.Emailfrom;
                 this.MailTo = r.Email ;
                 this.SmtpServer = emailsever.Smtp;
                 this.Password = emailsever.Password;
                 this.Body = string.Format(@"尊敬的 {0}用户，<br> &nbsp; &nbsp; &nbsp; &nbsp;祝贺您成为PBX的会员，PBX送您优惠卷{1}元，优惠卷编号：{2}", r.UserName, c.Amount, c.Couponno);
                 this.Subject = string.Format("PBX注册用户优惠卷 ");
                 this.sendMailMsg();
                 return true;
             }
             else
             {
                 return false;
             }

         }


         public bool RegConfirm(ResUser r, string createby )
         {
             Emailsever emailsever = EmailseverBLL.GetInstance().GetEmailseverByCreateBy(createby);

             if (r != null  )
             {
                 // this.Bcc = Email;
                 this.MailFrom = emailsever.Emailfrom;
                 this.MailTo = r.Email;
                 this.SmtpServer = emailsever.Smtp;
                 this.Password = emailsever.Password;
                 this.Body = string.Format(@"亲爱的{0}:<p>您已经在PBX网站申请注册，为了确认您的电子邮件地址并启动您的帐户,请单击以下链接：<p>
<a href='http://www.pbxaudio.com/reg1.aspx?guid={1}&username={0}' target='_blank'>点击这里激活帐户</a><p>
如果以上链接不能打开，请复制下方地址到您的浏览器地址栏，打开页面后同样可以完成帐户激活。<p> http://www.pbxaudio.com/reg1.aspx?guid={1}&username={0}<p> 
如您有疑问，请及时拨打我们的免费服务热线400-0061-661咨询。
", r.UserName, r.Guid);
                 this.Subject = string.Format("PBX用户注册确认邮件 ");
                 this.sendMailMsg();
                 return true;
             }
             else
             {
                 return false;
             }

         }




  }
}
